package library.gui.component;

import java.util.List;

import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import library.core.Library;
import library.core.book.Book;
import library.core.user.User;
import library.gui.SystemApplication;
import library.gui.dialog.ConfirmDialog;
import library.gui.dialog.NotiDialog;
import library.gui.dialog.PostDialog;
import library.utils.Info;

public class BookView extends BorderPane{

	private SystemApplication handler;
	
	private SearchPane searchPane;
	private BookList bookList;
	private Selector selector;

	static class Selector extends HButtonList {
		public Selector(Button...all) {
			super(all);
		}
	}

	public void refreshBookList() {
		Library library = handler.getLibrary();
		List<Book> list = library.getAllBooks();
		list = library.pickByAuthor(searchPane.getText("作者"), list);
		list = library.pickByName(searchPane.getText("书名"), list);
		if(searchPane.isSelected("我借阅的"))
			list = library.pickBorrowed(handler.getCurrentUser(), list);
		if(searchPane.isSelected("有库存"))
			list = library.pickAvailable(handler.getCurrentUser(), list);
		bookList.refresh(list);
	}
	
	private void unselect() {
		new NotiDialog("未选择书籍！", "点击书籍图标以选择").showAndWait();
	}
	
	public void delete(Library library, Book book) {
		Info info = library.deleteBook(book);
		if(info.isSuccess()) {
			new NotiDialog("删除成功！", info.toString()).showAndWait();
			refreshBookList();
		}
		else 
			new NotiDialog("删除失败！", info.toString()).showAndWait();
	}
	
	public void initSelector() {
		User user = handler.getCurrentUser();
		Library library = handler.getLibrary();
		if(user.isAdmin()) {
			selector = new Selector(
				new FunctionalButton("添加书籍", ()->{
					InputList inputList = new InputList()
						.addKey("书名").addKey("作者").addKey("类别").addKey("价格").addKey("库存");
					new PostDialog("添加书籍", inputList, 
						new FunctionalButton("取消", ()->{}),
						new FunctionalButton("确定", ()->{
							Info info = library.addBook(
								inputList.get("书名"), inputList.get("作者"), 
								inputList.get("类别"), inputList.get("价格"),
								inputList.get("库存"));
							if(info.isSuccess()) {
								new NotiDialog("添加成功！", info.toString()).showAndWait();
								refreshBookList();
							}
							else 
								new NotiDialog("添加失败！", info.toString()).showAndWait();
						})).showAndWait();
				}),
				new FunctionalButton("修改库存", ()->{
					Book book = bookList.getSelectedBook();
					if(book == null) {
						unselect();
						return;
					}
					InputList inputList = new InputList().addKey("库存");
					inputList.setDefaults(String.valueOf(book.getNum()));
					new PostDialog("修改库存", inputList, 
							new FunctionalButton("取消", ()->{}),
							new FunctionalButton("确定", ()->{
								Info info = book.modifyBookNum(inputList.get("库存"));
								if(info.isSuccess()) {
									new NotiDialog("修改成功！", info.toString()).showAndWait();
									refreshBookList();
								}
								else 
									new NotiDialog("修改失败！", info.toString()).showAndWait();
							})).showAndWait();
				}),
				new FunctionalButton("修改信息", ()->{
					Book book = bookList.getSelectedBook();
					if(book == null) {
						unselect();
						return;
					}
					InputList inputList = new InputList()
						.addKey("书名").addKey("作者").addKey("类别").addKey("价格")
						.setDefaults(book.getName(), book.getAuthor(), book.getType(), String.valueOf(book.getPrice()));
					new PostDialog("修改信息", inputList, 
						new FunctionalButton("取消", ()->{}),
						new FunctionalButton("确定", ()->{
							Info info = book.modifyBook(
								inputList.get("书名"), inputList.get("作者"), 
								inputList.get("类别"), inputList.get("价格"));
							if(info.isSuccess()) {
								new NotiDialog("修改成功！", info.toString()).showAndWait();
								refreshBookList();
							}
							else 
								new NotiDialog("修改失败！", info.toString()).showAndWait();
						})).showAndWait();
				}),
				new FunctionalButton("删除书籍", ()->{
					Book book = bookList.getSelectedBook();
					if(book == null) {
						unselect();
						return;
					}
					if(library.getBorrowedNum(book) > 0) {
						new ConfirmDialog("该书籍有读者借阅", "确认删除将一并抹除借阅信息")
							.listenOk(()->delete(library, book)).showAndWait();
					}
					else {
						new ConfirmDialog("单击确定删除书籍", "")
							.listenOk(()->delete(library, book)).showAndWait();
					}
				}));
		}
		else {
			selector = new Selector(
				new FunctionalButton("借书", ()->{
					Book book = bookList.getSelectedBook();
					if(book == null) {
						unselect();
						return;
					}
					Info info = library.borrow(user, book);
					if(info.isSuccess()) {
						new NotiDialog("借书成功！", info.toString()).showAndWait();
						refreshBookList();
					}
					else 
						new NotiDialog("借书失败！", info.toString()).showAndWait();
				}),
//				new FunctionalButton("续借", ()->{
//					Book book = bookList.getSelectedBook();
//					if(book == null) {
//						unselect();
//						return;
//					}
//					if(library.borrowed(user, book).no()) 
//						new NotiDialog("你未借阅该书！", "").showAndWait();
//					else {
//						Info info = library.borrow(user, book);
//						if(info.isSuccess()) {
//							new NotiDialog("续借成功！", info.toString()).showAndWait();
//							refreshBookList();
//						}
//						else 
//							new NotiDialog("续借失败！", info.toString()).showAndWait();
//					}
//				}),
				new FunctionalButton("还书", ()->{
					Book book = bookList.getSelectedBook();
					if(book == null) {
						unselect();
						return;
					}
					Info info = library.returnBook(user, book);
					if(info.isSuccess()) {
						new NotiDialog("还书成功！", info.toString()).showAndWait();
						refreshBookList();
					}
					else 
						new NotiDialog("还书失败！", info.toString()).showAndWait();
				}));
		}
	}
	
	private void initSearchPane() {
		this.searchPane = new SearchPane();
		User user = handler.getCurrentUser();
		if(user.isAdmin()) 
			this.searchPane.addTextKey("书名").addTextKey("作者")
			.addOptionKey("有库存");
		else 
			this.searchPane.addTextKey("书名").addTextKey("作者")
			.addOptionKey("我借阅的").addOptionKey("有库存");
		this.searchPane.listenSearch(this::refreshBookList);
	}
	
	public BookView(SystemApplication handler) {
		
		this.handler = handler;
		
		this.initSearchPane();
		this.bookList = new BookList(handler);
		this.bookList.prefWidthProperty().bind(this.widthProperty());
		refreshBookList();
		this.initSelector();
		
		this.setPadding(new Insets(5));
		
		this.setTop(this.searchPane);
		this.setCenter(bookList);
		this.setBottom(selector);
		
	}
	
}
